package com.cskaoyan.user.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.mall.commons.util.jwt.JwtTokenUtils;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dal.entitys.User;
import com.cskaoyan.user.dal.persistence.MemberMapper;
import com.cskaoyan.user.dto.UserLoginResponse;
import com.cskaoyan.user.dto.userToken;
import com.cskaoyan.user.form.KaptchaCodeRequest;
import com.cskaoyan.user.form.UserLoginRequest;
import com.cskaoyan.user.form.UserRegisterRequest;
import com.cskaoyan.user.service.IKaptchaService;
import com.cskaoyan.user.service.ILoginService;
import com.cskaoyan.user.service.IUserRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;

@RestController
@RequestMapping("user")
public class UserRegisterController {

    @Autowired
    IUserRegisterService iUserRegisterService;
    @Autowired
    ILoginService iLoginService;
    @Autowired
    IKaptchaService kaptchaService;
    /**
     * 用户注册
     *
     * @return {@link ResponseData}
     */
    @PostMapping("register")
    public ResponseData userRegister(@RequestBody UserRegisterRequest registerRequset,HttpServletRequest request){

        String kaptcha_uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        kaptchaCodeRequest.setUuid(kaptcha_uuid);
        kaptchaCodeRequest.setCode(registerRequset.getCaptcha());
        kaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        iUserRegisterService.register(registerRequset);
        ResponseUtil<Object> objectResponseUtil = new ResponseUtil<>();
        return objectResponseUtil.setData(null);
    }

    /**
     * 用户loginv
     *
     * @param request 请求
     * @return {@link ResponseData}
     */
    @GetMapping("login")
    public ResponseData userLoginV(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        Cookie cookie=null;
        for (int i = 0; i < cookies.length; i++) {
            if ("access_token".equals(cookies[i].getName())){
                cookie=cookies[i];
                break;
            }
        }
        String comment = cookie.getValue();
        System.out.println(comment);
        String msg = JwtTokenUtils.builder().token(comment).build().freeJwt();
        userToken userToken = iLoginService.loginV(msg);
        ResponseUtil<Object> objectResponseUtil = new ResponseUtil<>();
       return objectResponseUtil.setData(userToken,"success");
    }

    /**
     * 用户登录
     *
     * @param userLoginRequest 用户登录请求
     * @param response         响应
     * @return {@link ResponseData}
     */
    @PostMapping("login")
    public ResponseData userLogin(@RequestBody UserLoginRequest userLoginRequest,HttpServletRequest request, HttpServletResponse response){
        String kaptcha_uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        kaptchaCodeRequest.setUuid(kaptcha_uuid);
        kaptchaCodeRequest.setCode(userLoginRequest.getCaptcha());
        kaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        UserLoginResponse login = iLoginService.login(userLoginRequest);
        ResponseUtil<Object> objectResponseUtil = new ResponseUtil<>();
        response.addCookie(CookieUtil.genCookieWithDomain("access_token",login.getToken(),86400,"localhost"));
        return objectResponseUtil.setData(login,"success");
    }

    /**
     * 用户登录
     *
     * @param request 请求
     * @return {@link ResponseData}
     */
    @GetMapping("loginOut")
    public ResponseData userLoginOut(HttpServletRequest request,HttpServletResponse response){

        response.addCookie(CookieUtil.genCookieWithDomain("access_token",null,0,"localhost"));
        ResponseUtil<Object> objectResponseUtil = new ResponseUtil<>();
        return objectResponseUtil.setData(null);
    }


    /**
     * 用户验证
     *
     * @param uuid uuid
     * @return {@link ResponseData}
     */
    @GetMapping("verify")
    public ResponseData userVerify(String uuid){
        iUserRegisterService.userVerify(uuid);
        ResponseUtil<Object> objectResponseUtil = new ResponseUtil<>();
        return objectResponseUtil.setData(null);
    }




}
